<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\AuctionOrder\AuOrderList as AuOrderList;
use AuctionCore\System\AuctionException;

/**
 * 车源展示
 *
 * @author Xmh
 */
class CarShowController extends BaseController {

    private $CarSaleModel;
    private $CarsModel;

    public function _initialize() {
        $this->CarsModel = M("Cars");
        $this->CarSaleModel = M("CarSale");
    }

    public function __construct() {
        parent::__construct();
        //页面头部信息
        $head = C('HEAD_INDEX');
        $this->assign("web_title", $head['title']);
        $this->assign("web_keywords", $head['keywords']);
        $this->assign("web_description", $head['description']);
        $this->emp_id = $this->user_info['emp_id'];
        //模板赋值
        $this->assign("emp_id", $this->emp_id);
    }

    public function index() {
        $car_marks = C('CAR_MARKS');
        $this->assign("car_marks", $car_marks);
        $this->display();
    }

    /**
     * 获取车源销售列表
     */
    public function car_list() {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
//        $params['emp_id'] = $this->emp_id;
        $params['brand_id'] = I("get.brand_id");
        $params['series_id'] = I("get.series_id");
        $params['model_id'] = I("get.model_id");
        $params['model_id'] = I("get.model_id");
        $params['car_status'] = I("get.car_status", 1);  //状态：1待上架、2销售中、3销售成功（结束）
        $params['car_mark'] = I("get.car_mark", '');  //标签：车主寄售|高性价比|
        $params['car_no_state'] = I("get.car_no_state"); //车辆编号、等级
        //获取list
        $aol = new AuOrderList();
        $result = $aol->car_unsold_list($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();  // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign("car_status", $params['car_status']);
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    /**
     * 上架车源
     */
    public function up() {
        $p = I("get.p");
        $sale_id = I('get.sid');
        $car_id = I('get.car_id');
        $map['c.car_id'] = $car_id;
        $map['cs.car_status'] = 1;
        $cols = 'c.car_no,c.first_reg_date,c.pointer_mileage,c.emission,c.car_color,c.reg_city
                ,c.composite_state,c.accident_level,c.reserve_price,c.vin,c.plate_prefix,c.plate_suffix
                ,ow.seller_name,ow.phone,ow.car_model,ow.owner_type,ow.contact_person,
                ,cs.car_success_price,cs.reason,cs.car_status';
        $list = $this->CarsModel->alias('c')->field($cols)
                        ->join(C('DB_PREFIX') . "car_owner as ow ON c.owner_id= ow.owner_id")
                        ->join(C('DB_PREFIX') . "car_sale as cs ON c.car_id = cs.car_id")
                        ->where($map)->find();
        $city = new \AuctionCore\Other\Address();
        $reg_city = $city->get_name_by_code($list['reg_city']);
        $data['car_no'] = $list['car_no'];
        $data['car_status'] = $list['car_status'];
        $data['first_reg_date'] = $list['first_reg_date'];
        $data['first_reg_city'] = $reg_city;
        $data['car_color'] = C('CAR_COLOR')[$list['car_color']];
        $data['emission'] = C('EMISSION')[$list['emission']];
        $data['pointer_mileage'] = $list['pointer_mileage'];
        $data['chek_level'] = $list['composite_state'] . $list['accident_level'];
        $data['vin'] = $list['vin'];
        $data['chepai_hao'] = C('PLATE_PREFIX')[$list['plate_prefix']] . $list['plate_suffix'];
        $data['reserve_price'] = $list['reserve_price'];
        $data['seller_name'] = $list['seller_name'];
        $data['phone'] = $list['phone'];
        $data['owner_type_name'] = C('OWNER_TYPE_NAME')[$list['owner_type']];
        $data['car_model'] = $list['car_model'];

        $car_marks = C('CAR_MARKS');
        $this->assign("p", $p);
        $this->assign("car_marks", $car_marks);
        $this->assign("list", $data);
        $this->assign("sale_id", $sale_id);
        $this->display();
    }

    public function up_post() {
        $car['sale_id'] = I('post.sale_id');
        $car['emp_id'] = $this->emp_id;
        $car['car_marks'] = rtrim(I("post.car_marks"), "、");
        $car['car_status'] = 2;
        $car['car_uptime'] = date("Y-m-d H:i:s", time());
        $res = $this->CarSaleModel->save($car);
        if ($res) {
            //记录员工操作日志
            $log = array('emp_id' => $this->emp_id, 'action' => '上架车辆成功');
            \AuctionCore\System\SystemLog::add($log);
            $this->helpJsonResult(200, '上架成功！');
        } else {
            $this->helpJsonResult(500, '上架失败！');
        }
    }

    public function down() {
        $p = I("get.p");
        $sale_id = I('get.sid');
        $car_id = I('get.car_id');
        $map['c.car_id'] = $car_id;
//        $map['cs.car_status'] = 2;
        $cols = 'c.car_no,c.first_reg_date,c.pointer_mileage,c.emission,c.car_color,c.reg_city
                ,c.composite_state,c.accident_level,c.reserve_price,c.vin,c.plate_prefix,c.plate_suffix
                ,ow.seller_name,ow.phone,ow.car_model,ow.owner_type,ow.contact_person,
                ,cs.car_success_price,cs.reason,cs.car_status';
        $list = $this->CarsModel->alias('c')->field($cols)
                        ->join(C('DB_PREFIX') . "car_owner as ow ON c.owner_id= ow.owner_id")
                        ->join(C('DB_PREFIX') . "car_sale as cs ON c.car_id = cs.car_id")
                        ->where($map)->find();
        $city = new \AuctionCore\Other\Address();
        $reg_city = $city->get_name_by_code($list['reg_city']);
        $data['car_no'] = $list['car_no'];
        $data['car_status'] = $list['car_status'];
        $data['first_reg_date'] = $list['first_reg_date'];
        $data['first_reg_city'] = $reg_city;
        $data['car_color'] = C('CAR_COLOR')[$list['car_color']];
        $data['emission'] = C('EMISSION')[$list['emission']];
        $data['pointer_mileage'] = $list['pointer_mileage'];
        $data['chek_level'] = $list['composite_state'] . $list['accident_level'];
        $data['vin'] = $list['vin'];
        $data['chepai_hao'] = C('PLATE_PREFIX')[$list['plate_prefix']] . $list['plate_suffix'];
        $data['reserve_price'] = $list['reserve_price'];
        $data['seller_name'] = $list['seller_name'];
        $data['phone'] = $list['phone'];
        $data['owner_type_name'] = C('OWNER_TYPE_NAME')[$list['owner_type']];
        $data['car_model'] = $list['car_model'];
        $this->assign("p", $p);
        $this->assign("list", $data);
        $this->assign("sale_id", $sale_id);
        $this->display();
    }

    /**
     * 下架车源
     */
    public function down_post() {
        $type = I('post.type');
        $sale_id = I("post.sale_id");
        $car_id = I("post.car_id");
        $emp_id = $this->emp_id;
        if ($type == 1) {
            $data['sale_id'] = $sale_id;
            $data['emp_id'] = $emp_id;
            $data['car_status'] = 3;
            $data['reason'] = I('post.success_remark');
            $data['car_success_price'] = I('post.success_price');
            $data['car_downtime'] = date("Y-m-d H:i:s", time());
        } elseif ($type == 2) {
            $data['sale_id'] = $sale_id;
            $data['emp_id'] = $emp_id;
            $data['car_status'] = 4;
            $data['reason'] = I('fail_reason');
            $data['car_downtime'] = date("Y-m-d H:i:s", time());
        }
        $res = $this->CarSaleModel->save($data);
        if ($res) {
            //记录员工操作日志
            $log = array('emp_id' => $this->emp_id, 'action' => '下架车辆成功');
            \AuctionCore\System\SystemLog::add($log);
            $this->helpJsonResult(200, '下架成功！');
        } else {
            $this->helpJsonResult(500, '下架失败！');
        }
    }

    /**
     * 重新到待上架状态
     */
    public function back_up() {
        $sale_id = I('post.sale_id');
        $sale['sale_id'] = $sale_id;
        $sale['car_status'] = 1;
        $sale['car_marks'] = '';
        $res = $this->CarSaleModel->save($sale);
        if ($res) {
            //记录员工操作日志
            $log = array('emp_id' => $this->emp_id, 'action' => '重新到待上架成功！');
            \AuctionCore\System\SystemLog::add($log);
            $this->helpJsonResult(200, '重新到待上架成功！');
        } else {
            $this->helpJsonResult(500, '重新到待上架失败！');
        }
    }

    /**
     * 查看
     */
    public function check() {
        $p = I("get.p");
        $sale_id = I('get.sid');
        $car_id = I('get.car_id');
        $cols = 'c.car_no,c.first_reg_date,c.pointer_mileage,c.emission,c.car_color,c.reg_city
                ,c.composite_state,c.accident_level,c.reserve_price,c.vin,c.plate_prefix,c.plate_suffix
                ,ow.seller_name,ow.phone,ow.car_model,ow.owner_type,ow.contact_person,
                ,cs.car_success_price,cs.reason,cs.car_status';
        $list = $this->CarsModel->alias('c')->field($cols)
                        ->join(C('DB_PREFIX') . "car_owner as ow ON c.owner_id= ow.owner_id")
                        ->join(C('DB_PREFIX') . "car_sale as cs ON c.car_id = cs.car_id")
                        ->where('c.car_id=' . $car_id)->find();
        $city = new \AuctionCore\Other\Address();
        $reg_city = $city->get_name_by_code($list['reg_city']);
        $data['car_no'] = $list['car_no'];
        $data['first_reg_date'] = $list['first_reg_date'];
        $data['first_reg_city'] = $reg_city;
        $data['car_color'] = C('CAR_COLOR')[$list['car_color']];
        $data['emission'] = C('EMISSION')[$list['emission']];
        $data['pointer_mileage'] = $list['pointer_mileage'];
        $data['chek_level'] = $list['composite_state'] . $list['accident_level'];
        $data['vin'] = $list['vin'];
        $data['car_success_price'] = format_money($list['car_success_price']);
        $data['reason'] = $list['reason'];
        $data['car_status'] = $list['car_status'];
        $data['chepai_hao'] = C('PLATE_PREFIX')[$list['plate_prefix']] . $list['plate_suffix'];
        $data['reserve_price'] = format_money($list['reserve_price']);
        $data['seller_name'] = $list['seller_name'];
        $data['phone'] = $list['phone'];
        $data['owner_type_name'] = C('OWNER_TYPE_NAME')[$list['owner_type']];
        $data['car_model'] = $list['car_model'];
//        echo "<pre>";
//        print_r($data);
//        echo "</pre>";
        $this->assign("p", $p);
        $this->assign("list", $data);
        $this->assign("sale_id", $sale_id);
        $this->display();
    }

    /**
     * 把磋商失败数据添加到sale表中
     */
    public function pl_insert() {
        $this->test();
    }

    private function test() {
        $cols = "DISTINCT(c.car_id),c.car_id";
        $map['o.status'] = array('eq', 12);
        $map['o.fail_type'] = array('eq', 6);
        $map['o.unsold_service_id'] = array('gt', 0);
        $list = M('Cars')->alias('c')->field($cols)
                ->join(C('DB_PREFIX') . "order AS o ON c.car_id=o.car_id")
                ->where($map)
                ->select();

        $sale_list = M('CarSale')->field('car_id')->select();
        foreach ($sale_list as $sl) {
            $sale_arr[] = $sl['car_id'];
        }
//        echo "<pre>";
//        print_r($list);
//        print_r($sale_list);
//        echo "</pre>";
        foreach ($list as $v) {
            if (!in_array($v['car_id'], $sale_arr)) {
                $sale['car_id'] = $v['car_id'];
                $sale['car_status'] = '1';  //待拍卖
//                echo "<pre>";
//                print_r($sale);
//                echo "</pre>";
                M('CarSale')->add($sale);
            }
        }
    }

}
